package com.hitqz.robot.biz.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hitqz.robot.api.business.entity.GoodsStoreLogEntity;
import com.hitqz.robot.api.business.params.QueryGoodsStoreLogParams;
import com.hitqz.robot.api.business.vo.GoodsStoreLogVo;
import com.hitqz.robot.biz.service.GoodsStoreLogService;
import com.hitqz.robot.common.core.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author xupkun
 * @date 2025/1/3
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/goodsStoreLog")
@Tag(name = "缓存架管理-日志")
@Slf4j
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class GoodsStoreLogController {

    private final GoodsStoreLogService goodsStoreLogService;

    @Operation(summary = "分页获取缓存-日志信息")
    @GetMapping
    public R<Page<GoodsStoreLogVo>> page(@ParameterObject QueryGoodsStoreLogParams query) {
        Page<GoodsStoreLogEntity> page = new Page<>(query.getCurrent(),query.getSize());
        LambdaQueryWrapper<GoodsStoreLogEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(GoodsStoreLogEntity::getGoodsBufferId, query.getGoodsId());
        wrapper.orderByDesc(GoodsStoreLogEntity::getId);
        Page<GoodsStoreLogEntity> goodsStoreLogEntityPage = goodsStoreLogService.page(page, wrapper);

        Page<GoodsStoreLogVo> voPage = new Page<>(page.getCurrent(), page.getSize());
        List<GoodsStoreLogVo> robotVos = Lists.newArrayList();
        goodsStoreLogEntityPage.getRecords().forEach(t-> {
            GoodsStoreLogVo vo = new GoodsStoreLogVo();
            BeanUtil.copyProperties(t, vo, false);
            robotVos.add(vo);
        });
        voPage.setTotal(goodsStoreLogEntityPage.getTotal());
        voPage.setRecords(robotVos);
        return R.ok(voPage);
    }

}
